<template>
	<view class="addressslist">
		<view class="addressbox">
			<view v-if="addressList.length>0" class="addresssBox">
				<view class="addresssli"  v-for="(item,index) in addressList" :key="index" @tap="selectAddress(item)">
					<view class="addressslicon">
						<view class="addresssliname">
							<view>收获人：&nbsp;{{item.name}}&nbsp;
							<text class="addressslinameDefault" v-if="item.isDefault==1">默认</text>
							</view>
							<span>{{item.mobile}}</span>
						</view>
						<view class="addresssliinfo">
							<view class="addresssliinfotext">
								<view class="addresssliinfotextl">
									收货地址：
								</view>
								<view class="">
									{{item.province+item.city+item.country+item.detail}}
								</view>
							</view>
						</view>
					</view>
					<view class="receipt">
						<view class="receiptl" @click.stop="item.isDefault!=1?receiptlFun(item.id,index):''">
							<image class="receiptlImg" v-if="item.isDefault==1" :src="imgUrl+'icon2.png'" mode="widthFix"></image>
							<image class="receiptlImg" v-else :src="imgUrl+'icon1.png'" mode="widthFix"></image>
							设置默认
						</view>
						<view class="receiptlist">
							<view class="addresssliedit" @tap.stop="editFun(item)">编辑</view>
							<view class="addresssliedit receiptliedit" v-if="item.isDefault!=1" @tap.stop="deleteFun(item.id)">删除</view>
						</view>
					</view>
				</view>
			</view>
			<sunui-template v-else title="快去添加地址吧" />
		</view>
		<view class="delgoods" v-if="delgoodsshow">
			<view class="delgoodsctn">
				<view class="delgoodstit">确认删除该地址吗?</view>
				<view class="delgoodsbtn">
					<view @click="closeshow">取消</view>
					<view @click="DelGoodsFun">确定</view>
				</view>
			</view>
		</view>
		<view class="addaddressbtn">
			<view @tap="editFun()">
				+新增地址
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgUrl:this.$apiFun.imgUrl,
				addressList:{},
				setData:{},
				current:1,
				delgoodsshow:false,
				deleteFunid:'',
				order_id:'',//订单id
				shopId:'',//门店id
			}
		},
		onShow(){
			this.init();
		},
		onLoad(option) {
			if(option.order_id&&option.shopId){
				// 存在则为提交订单选择地址进来的，返回时续携带参数返回
				this.order_id = option.order_id
				this.shopId = option.shopId
			}
		},
		methods: {
			//初始化方法
			init(){
				var that = this;
				var data = {}
				this.$apiFun.getApi(this.$api.userAddressQueryListApi,data,function(res){
					if(res.code ==200){
						that.addressList = res.data
					}
				},function(err){
					uni.showToast({
						icon: 'none',
						title: err.msg
					})
				});
			},
			// 返回订单页
			selectAddress(item){
				// if(this.shopId){
				// 	this.apply_alter_addressApi(item.id)
				// 	uni.navigateTo({
				// 		url:'/pages_goodsClass/Shopcart/uni_Orderconfirm?obj='+encodeURIComponent(JSON.stringify(item))
				// 	})
				// }
			},
			// 新增修改接口
			editFun(item){
				let url = item?'uni_Address?obj='+encodeURIComponent(JSON.stringify(item)):'uni_Address'
				uni.navigateTo({
				    url: url
				});
			},
			DelGoodsFun(){
				const that = this
				const data = {id:this.deleteFunid}
				this.$apiFun.postApi(this.$api.userAddressDelApi,data,function(res){
					if(res.code ==200){
						that.init();
						uni.showToast({
							icon: 'none',
							title: "删除成功"
						})
						that.delgoodsshow = false;
					}
				},function(err){
					uni.showToast({
						icon: 'none',
						title: err.msg
					})
				})
			},
			closeshow(){
				this.delgoodsshow = false;
			},
			deleteFun(id){
				this.deleteFunid = id;
				this.delgoodsshow = true;
			},
			receiptlFun(id,index){
				this.address_defaultApi(id)
			},
			address_defaultApi(id){
				var that = this;
				var data = {id:id}
				this.$apiFun.postApi(this.$api.setDefaultApi,data,function(res){
					if(res.code ==200){
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
						setTimeout(()=>{
							that.init()
						},1000)
					}
				},function(err){
					uni.showToast({
						icon: 'none',
						title: err.msg
					})
				});
			},
		}
	}
</script>

<style scoped lang="less">
	page{
		height: 100%;
		width: 100%;
	}
	.addressslist{
		height: 100%;
		box-sizing: border-box;
		background-color: #eeeeee;
		.addressbox{
		height: 100%;
		}
	}
	.addresssBox{
		padding:5rpx 15rpx 150rpx;
	}
	.addresssli{
		width: 100%;
		overflow: hidden;
		background-color: #ffffff;
		padding: 38rpx 0 30rpx;
		margin: 20rpx 0;
		border-radius: 6rpx;
		.addressslicon{
			padding: 0 30rpx 30rpx;
			box-sizing: border-box;
			border-bottom: 1px solid #cccccc;
		}
		
		.receipt{padding: 30rpx 30rpx 0;overflow: hidden;height: auto;}
		.receiptl{
			float: left;
			box-sizing: border-box;
			background-position:left center;
			background-repeat: no-repeat;
			background-size: 25rpx;
			font-size: 24rpx;
			color: #333333;
			display:flex;
			.receiptlImg{
				width:35rpx;
				height:35rpx;
			}
		}
		.receiptlist{
			float: right;
			display: flex;
		}
		.addresssliedit{
			width: 80rpx;
			box-sizing: border-box;
			background-position:left center;
			background-repeat: no-repeat;
			background-size: 25rpx;
			text-align: right;
			font-size: 24rpx;
			color: #333333;
		}
		.receiptliedit{
			margin-left: 20rpx;
		}
		.addresssliname{
			height: 28rpx;
			line-height: 28rpx;
			font-size: 26rpx;
			color: #333333;
			margin-bottom: 30rpx;
			display:flex;
			justify-content: space-between;
			.addressslinameDefault{
				font-size: 22rpx;
				color:#e42e3c;
				padding:2rpx 5rpx;
				border-radius:6rpx;
				border:1rpx solid #e42e3c;
				margin-left:5rpx;
			}
		}
		.addresssliinfo{
			display: flex;
			font-size: 26rpx;
			line-height: 30rpx;
			.addresssliinfotext{
				display: flex;
				.addresssliinfotextl{
					white-space: nowrap;
				}
				.addresssliinfotextr{
					flex: 1;
				}
			}
		}
		
		
	}
	.addaddressbtn{
		padding: 0 30px;
		box-sizing: border-box;
		overflow: hidden;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		height: 150rpx;
		background: #ffffff;
		display: flex;
	}
	.addaddressbtn view{
		margin: auto;
		width: 100%;
		height: 80rpx;
		color: #FFFFFF;
		font-size: 24rpx;
		float: left;
		border-radius: 10rpx;
		text-align: center;
		line-height: 80rpx;
	}
	.addaddressbtn view:nth-child(2){
		margin-right: 0;
		background-color: #22ac38;
	}
	.addaddressbtn view:nth-child(1){
		background-color: #e42e3c;
		border-radius: 40rpx;
	}
	.delgoods{width: 100%;height: 100%;background-color: rgba(0,0,0,0.5);overflow: hidden;position: fixed;top: 0;left: 0;margin: auto;z-index: 1000;}
	.delgoodsctn{width: 80%;height: 300rpx;background-color: #FFFFFF;position: absolute;top: 0;left: 0;right: 0;bottom: 0;margin: auto;padding: 5%;}
	.delgoodstit{width: 100%;height: 200rpx;line-height: 200rpx; overflow: hidden;text-align: center;color: #333333;font-size: 34rpx;}
	.delgoodsbtn{height: 100rpx;overflow: hidden;text-align: center;}
	.delgoodsbtn view{width: 150rpx;height: 70rpx;display: inline-block;line-height: 70rpx;background-color: #f6f6f6;margin-right: 50rpx;color: #333333;font-size: 30rpx;}
	.delgoodsbtn view:nth-child(2){background-color: #EB453C;color: #FFFFFF;margin-right: 0;}
</style>
